package view;

import commen.check;
import dao.DBUtil;
import dao.User;
import dao.blog;
import dao.blogdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

@WebServlet("/blog_edit")
public class blog_edit extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/http;charset=utf-8");
        User user=check.checkSession(req);
        if(user==null){
            resp.sendRedirect("login.html");
            return;
        }
        String title=  req.getParameter("title");
        String content=  req.getParameter("content");
        System.out.println(title);
        System.out.println(content);
        if(title==null||title.equals("")||content==null||content.equals("")){
            String html="<h3> 标题或内容不存在 </h3>";
            resp.getWriter().write(html);
            return;
        }
        blog blog=new blog();
        blog.setContent(content);
        blog.setTitle(title);
        blog.setUseId(user.getUserId());
        blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blogdao.insert(blog);
        //完后进行重定向
        resp.sendRedirect("blog_list.html");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("<h3>请先登录</h3>");
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        resp.sendRedirect("blog_edit");
    }
}
